<?php


    // Author: Tomasz Durka
    // Date: 4th March 2007
    // form_element
    
    
  function form_element($type, array $params = array()) {  
    
    $pattern = '/([a-zA-Z]+)(\(([0-9]+)\))?/';
    preg_match($pattern, $type, $matches);
    $type = $matches[1];
    $max_size = $matches[3];
    if ($max_size > 80)
      $type = 'less_than_text';
    
    switch ($type) {
      default:
        $type = 'varchar';
        $name = '';
        $value = '';
        $id = '';
        $class = '';
        $style = '';
        $multiline = false;
        break;
      case 'tinytext':
      case 'mediumtext':
      case 'text':
      case 'longtext':
      case 'tinyblob':
      case 'mediumblob':
      case 'blob':
      case 'longblob':
        //$style = ' style="width: 400px; height: 150px;"';
        break;
      case 'tinyint':
      case 'smallint':
      case 'mediumint':
      case 'int':
      case 'bigint':
      case 'float':
      case 'dobule':
      case 'decimal':
      case 'char':
      case 'varchar':
      case 'binary':
      case 'varbinary':
        $size = 'size="' . $max_size . '" ';
        break;
      case 'less_than_text':
        $style = 'style="width: 400px; height: 75px;"';
        break;
      case 'datetime':
      case 'date':
      case 'time':
        break;
    }
    
    foreach($params as $_key => $_val) {
      switch ($_key) {
        case 'type':
          break;
        case 'id':
        case 'class':
        case 'style':
          $$_key = " {$_key}=\"{$_val}\"";
          break;
        case 'multiline':
          $$_key = (bool)$_val;
          break;
        default:
          $$_key = $_val;
          break;
      }
    }
    
    switch ($type) {
      case 'tinytext':
      case 'mediumtext':
      case 'text':
      case 'longtext':
      case 'tinyblob':
      case 'mediumblob':
      case 'blob':
      case 'longblob':
      case 'less_than_text':
        $returned = "<textarea cols=\"50\" rows=\"8\" name=\"{$name}\"{$class}{$style}{$id}>{$value}</textarea>";
        break;
      case 'tinyint':
      case 'smallint':
      case 'mediumint':
      case 'int':
      case 'bigint':
      case 'float':
      case 'dobule':
      case 'decimal':
      case 'char':
      case 'varchar':
      case 'binary':
      case 'varbinary':
        $returned = '<input type="text" name="' . $name . '"' . 'value="' . $value . '" ' . $class . $style . $id . $size .' />';
        break;
      case 'datetime':
        $new_params = array('time' => $value, 'field_array' => $name);
        require_once $smarty->_get_plugin_filepath('function','html_select_datetime');
        $returned .= smarty_function_html_select_datetime($new_params, $smarty);
        break;
      case 'date':
        $new_params = array('time' => $value, 'field_array' => $name);
        require_once $smarty->_get_plugin_filepath('function','html_select_date');
        $returned .= smarty_function_html_select_date($new_params, $smarty);
        break;
      case 'time':
        $new_params = array('time' => $value, 'field_array' => $name);
        require_once $smarty->_get_plugin_filepath('function','html_select_time');
        $returned .= smarty_function_html_select_time($new_params, $smarty);
        break;
    }
    
    
    return $returned;
    
  }

?>